﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Prisa.Entities;
using Prisa.BussinesLogic;
using Microsoft.Practices.Unity;
using System.Text.RegularExpressions;

namespace Prisa.UI
{
    public partial class FrmMttoProveedor : Form
    {
        [Dependency]
        public IProveedorBL proveedorBL { get; set; }
        bool bandera;

        public FrmMttoProveedor()
        {
            InitializeComponent();
            this.BuildUp();
        }

        public PROVEEDOR CurrentProveedor { get; set; }

        public void Nuevo()
        {
            bandera = true;
            pROVEEDORBindingSource.AddNew();
        }

        public void Editar()
        {
            HabilitarTextBoxs();
            bandera = false;
            pROVEEDORBindingSource.DataSource = CurrentProveedor;
        }

        private void FrmMttoProveedor_Load(object sender, EventArgs e)
        {

        }

        private void BtnGuardar_Click(object sender, EventArgs e)
        {
            PROVEEDOR currentProveedor = pROVEEDORBindingSource.Current as PROVEEDOR;
            var msg = "ERROR";
            
            if (currentProveedor != null)
            {
                currentProveedor.RazonSocial = razonSocialTextBox.Text.ToUpper();
                currentProveedor.Direccion = direccionTextBox.Text.ToUpper();
                if (bandera == true)
                {
                    try
                    {
                        proveedorBL.AddProveedor(currentProveedor);
                        MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }

                }
                if (bandera == false)
                {
                    try
                    {
                        proveedorBL.UpdateProveedor(currentProveedor);
                        MessageBox.Show("Modificación Correcta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        Close();
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                        MessageBox.Show(msg, "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    
                }
            }
        }

        private void HabilitarTextBoxs()
        {
            razonSocialTextBox.Enabled = true;
            direccionTextBox.Enabled = true;
            telefonoTextBox.Enabled = true;
        }

        private void InhabilitarTextBoxs()
        {
            razonSocialTextBox.Enabled = false;
            direccionTextBox.Enabled = false;
            telefonoTextBox.Enabled = false;
        }

        private void BtnVerificar_Click(object sender, EventArgs e)
        {
            if (rucTextBox.Text.Length > 0)
            {
                VerificarRuc();
                razonSocialTextBox.Focus();
            }
            else
            {
                MessageBox.Show(this, "El RUC es requerido", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                razonSocialTextBox.Focus();
            }
        }

        private void rucTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rucTextBox.Text.Length > 0)
                {
                    VerificarRuc();
                    razonSocialTextBox.Focus();
                }
                else
                {
                    MessageBox.Show(this, "El RUC es requerido", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    razonSocialTextBox.Focus();
                }
            }
        }

        private void rucTextBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Tab)
            {
                if (rucTextBox.Text.Length > 0)
                {
                    VerificarRuc();
                }
                else
                {
                    MessageBox.Show(this, "El RUC es requerido", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    razonSocialTextBox.Focus();
                }
            }
        }

        private void VerificarRuc()
        {
        
            #region Add
            if (bandera)
            {
                if (rucTextBox.Text.Length == 8 || rucTextBox.Text.Length == 11)
                {
                    var _proveedor = proveedorBL.GetFromProveedorByRuc(rucTextBox.Text);
                    if (_proveedor != null)
                    {
                        pROVEEDORBindingSource.DataSource = _proveedor;

                        DialogResult rpta = MessageBox.Show(this, "Número de RUC no disponible", "Mensaje",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                        if (rpta == DialogResult.OK)
                        {
                            Close();
                        }
                    }
                    else
                    {
                        MessageBox.Show(this, "Número de RUC disponible", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        HabilitarTextBoxs();
                    }
                }
                else
                {
                    MessageBox.Show(this, "Longitud de RUC incorrecta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    rucTextBox.Focus();
                    InhabilitarTextBoxs();
                }
            }
            #endregion
            #region Update
            else
            {
                var _proveedor = pROVEEDORBindingSource.Current as PROVEEDOR;

                var existe = proveedorBL.ExistDuplicidadRucRazonSocial(rucTextBox.Text, razonSocialTextBox.Text, _proveedor.Id);
                if (existe)
                {
                    DialogResult rpta = MessageBox.Show(this, string.Format("Ya existe un Proveeedor con la misma información"),
                        "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(this, "Proveedor disponible", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            #endregion

        }

        private void rucTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsNumber(e.KeyChar)) && (e.KeyChar != (char)Keys.Back)
                && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear))
            {
                e.Handled = true;
                return;
            }
        }

        public int VecesNumeral = 0;
        public int VecesAsterisc = 0;

        private void telefonoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var numeral = Convert.ToString("#");
            var asterisc = Convert.ToString("*");
            string cadena = numeral + asterisc + (char)8;

            if (numeral.Contains(e.KeyChar) || asterisc.Contains(e.KeyChar))
            {
                VecesNumeral++;

                if (VecesNumeral == 1 || VecesAsterisc == 1)
                {
                    if (!cadena.Contains(e.KeyChar) && !(char.IsNumber(e.KeyChar)) && (e.KeyChar != (char)Keys.Back)
                        && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear))
                    {
                        e.Handled = true; return;
                    }
                }
                else
                {
                    if (!(char.IsNumber(e.KeyChar)) && (e.KeyChar != (char)Keys.Back)
                        && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear))
                    {
                        e.Handled = true; return;
                    }
                }
            }
            else
            {
                if (!(char.IsNumber(e.KeyChar)) && (e.KeyChar != (char)Keys.Back)
                        && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear))
                {
                    e.Handled = true;
                    return;
                }
                VecesNumeral = 0;
            }
           
        }

        private void razonSocialTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            var guion = Convert.ToString("-");
            var punto = Convert.ToString(".");
            var amberson = Convert.ToString("&");

            string cadena = guion + amberson + punto + (char)8;

            if (!cadena.Contains(e.KeyChar) && !(char.IsLetter(e.KeyChar)) && !(char.IsNumber(e.KeyChar))
                && (e.KeyChar != (char)Keys.Back) && (e.KeyChar != (char)Keys.Enter) && (e.KeyChar != (char)Keys.Clear)
                && !(char.IsSeparator(e.KeyChar)))
            {
                e.Handled = true;
                return;
            }
        }
             

        
        
    }
}
